import {
  CatalogModel
} from '../../models/catalog.js';

import {
  GoodsModel
} from '../../models/goods.js';

const catalogModel = new CatalogModel();
const goodsModel = new GoodsModel();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList: [],
    currentCategory: {},
    goodsCount: [],
    id: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    });
    catalogModel.getCatalog().then(res => {
      this.setData({
        categoryList: res.data.categoryList,
        currentCategory: res.data.currentCategory
      })
      wx.hideLoading();
    })

    goodsModel.getGoodsCount().then(res => {
      this.setData({
        goodsCount: res.data.goodsCount
      })
    })
  },

  onSwitchCate: function(event) {
    let currentTarget = event.currentTarget;
    if(this.data.currentCategory.id == currentTarget.dataset.id) {
      return;
    }

    this.getCurrentCategory(currentTarget.dataset.id);
  },

  getCurrentCategory: function(p_id) {
    wx.showLoading({
      title: '加载中...',
    });
    catalogModel.getCurrentCategory(p_id).then(res => {
      this.setData({
        currentCategory: res.data.currentCategory
      })
      wx.hideLoading();
    })
  }
})